#
#	File:    	Makefile
#	Package: 	Apple CarPlay Communication Plug-in.
#	Abstract: 	n/a 
#	Version: 	450.14
#	
#	Disclaimer: IMPORTANT: This Apple software is supplied to you, by Apple Inc. ("Apple"), in your
#	capacity as a current, and in good standing, Licensee in the MFi Licensing Program. Use of this
#	Apple software is governed by and subject to the terms and conditions of your MFi License,
#	including, but not limited to, the restrictions specified in the provision entitled ”Public 
#	Software”, and is further subject to your agreement to the following additional terms, and your 
#	agreement that the use, installation, modification or redistribution of this Apple software
#	constitutes acceptance of these additional terms. If you do not agree with these additional terms,
#	please do not use, install, modify or redistribute this Apple software.
#	
#	Subject to all of these terms and in consideration of your agreement to abide by them, Apple grants
#	you, for as long as you are a current and in good-standing MFi Licensee, a personal, non-exclusive 
#	license, under Apple's copyrights in this original Apple software (the "Apple Software"), to use, 
#	reproduce, and modify the Apple Software in source form, and to use, reproduce, modify, and 
#	redistribute the Apple Software, with or without modifications, in binary form. While you may not 
#	redistribute the Apple Software in source form, should you redistribute the Apple Software in binary
#	form, you must retain this notice and the following text and disclaimers in all such redistributions
#	of the Apple Software. Neither the name, trademarks, service marks, or logos of Apple Inc. may be
#	used to endorse or promote products derived from the Apple Software without specific prior written
#	permission from Apple. Except as expressly stated in this notice, no other rights or licenses, 
#	express or implied, are granted by Apple herein, including but not limited to any patent rights that
#	may be infringed by your derivative works or by other works in which the Apple Software may be 
#	incorporated.  
#	
#	Unless you explicitly state otherwise, if you provide any ideas, suggestions, recommendations, bug 
#	fixes or enhancements to Apple in connection with this software (“Feedback”), you hereby grant to
#	Apple a non-exclusive, fully paid-up, perpetual, irrevocable, worldwide license to make, use, 
#	reproduce, incorporate, modify, display, perform, sell, make or have made derivative works of,
#	distribute (directly or indirectly) and sublicense, such Feedback in connection with Apple products 
#	and services. Providing this Feedback is voluntary, but if you do provide Feedback to Apple, you 
#	acknowledge and agree that Apple may exercise the license granted above without the payment of 
#	royalties or further consideration to Participant.
#	
#	The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR 
#	IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY 
#	AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR
#	IN COMBINATION WITH YOUR PRODUCTS.
#	
#	IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES 
#	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#	PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION 
#	AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
#	(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 
#	POSSIBILITY OF SUCH DAMAGE.
#	
#	Copyright (C) 2012-2021 Apple Inc. All Rights Reserved. Not to be used or disclosed without permission from Apple.
#
#	Build options
#	-------------
#	arch		-- "arm" for ARMv7, "arm64 for ARM aarch64, "x86" for x86.
#	debug		-- 1=Compile in debug code, asserts, etc. 0=Strip out debug code for a release build.
#	linux		-- 1=Build for Linux.
#	nobonjour	-- 1=Do not include Bonjour support.
#	nv			-- 1=Build for NVIDIA Jetson reference board.
#	openssl		-- 1=Use OpenSSL for AES, SHA*, etc. 0=Compile in AES, SHA*, etc. code directly.
#	qnx			-- 1=Build for QNX.
#	rpi			-- 1=Build using RPi toolchain on Linux host
#	stub		-- 1=Build AudioUtils/ScreenUtils/AudioConverter stub DLLs.
#	verbose		-- 1=Produce verbose output.
#

INTERACTIVE := $(shell [ -t 0 ] && echo 1)
ifdef INTERACTIVE
	ColorMagenta=\033[0;35m
	ColorCyan=\033[0;36m
	ColorEnd=\033[0m
endif

# Build options

	hid					?= 1
	nohidraw			?= 1
	screen				?= 1
ifeq ($(debug),1)
	config				= Debug
else
	config				= Release
endif
ifeq ($(nv),1)
	linux				?= 1
	os					= linux
endif
ifeq ($(qnx),1)
	os					= qnx
	arch				= arm
endif
ifeq ($(rpi),1)
	os					= linux
endif
ifeq ($(os),linux)
	linux				= 1
endif
os						?= unknown
stub					?= 1
ifneq ($(verbose),1)
	quiet				= @
endif

# Build tools

ifeq ($(nv),1)
	# Use NVidia's Vibrante toolchain.

	ifndef NVTOOLSPATH
		$(error NVTOOLSPATH must be set to the location of the Vibrante toolchain)
	endif
	include $(NVTOOLSPATH)/make/nvdefs.mk

	CFLAGS				+= $(NV_PLATFORM_OPT) $(NV_PLATFORM_CFLAGS)
	CPPFLAGS			+= $(NV_PLATFORM_SDK_INC) $(NV_PLATFORM_CPPFLAGS)
	LINKFLAGS			+= $(NV_PLATFORM_SDK_LIB) $(NV_PLATFORM_LDFLAGS)
else
	ifeq ($(qnx),1)
		# Use the QNX toolchain.

		qnx_arch		= gcc_ntoarmv7le
		qnx_prefix		= ntoarmv7

		AR				= $(qnx_prefix)-ar
		CC				= qcc -V$(qnx_arch)
		CXX				= qcc -V$(qnx_arch)_cpp
		LD				= $(qnx_prefix)-ld
		NM				= $(qnx_prefix)-nm
		RANLIB			= $(qnx_prefix)-ranlib
		STRIP			= $(qnx_prefix)-strip

		CFLAGS			+= -w1
		CXXFLAGS		+= -w1
		LINKFLAGS		+= -w1
	else ifeq ($(rpi),1)
		# Cross compilation for Raspberry Pi 
		ifeq ($(arch),arm)
			CROSSBASE		= $(LINUX_TOOLS_DIR)
			CROSSBASEREAL	= $(realpath $(CROSSBASE))
			CROSSPREFIX		= $(CROSSBASEREAL)/bin/arm-linux-gnueabihf-
			CROSSROOT		= $(CROSSBASEREAL)/arm-linux-gnueabi/libc
			CROSS_INC		= $(CROSSROOT)/usr/include
			CROSS_LIB		= $(CROSSROOT)/lib
			CROSS_ULIB		= $(CROSSROOT)/usr/lib

			CFLAGS			+= -I$(CROSS_INC)
			CPPFLAGS		+= -I$(CROSS_INC)
			LINKFLAGS		+= -L$(CROSS_LIB) -L$(CROSS_ULIB)

			AR				= $(CROSSPREFIX)ar
			CC				= $(CROSSPREFIX)gcc
			CXX				= $(CROSSPREFIX)g++
			LD				= $(CROSSPREFIX)gcc
			NM				= $(CROSSPREFIX)nm
			RANLIB			= $(CROSSPREFIX)ranlib
			STRIP			= $(CROSSPREFIX)strip
		else ifeq ($(arch),arm64)
			CROSSBASE		= $(LINUX_TOOLS_DIR)
			CROSSBASEREAL	= $(realpath $(CROSSBASE))
			CROSSPREFIX		= $(CROSSBASEREAL)/bin/aarch64-linux-gnu-
			CROSSROOT		= $(CROSSBASEREAL)/aach64-linux-gnu/libc
			CROSS_INC		= $(CROSSROOT)/usr/include
			CROSS_LIB		= $(CROSSROOT)/lib64
			CROSS_ULIB		= $(CROSSROOT)/usr/lib64

			CFLAGS			+= -I$(CROSS_INC)
			CPPFLAGS		+= -I$(CROSS_INC)
			LINKFLAGS		+= -L$(CROSS_LIB) -L$(CROSS_ULIB)

			AR				= $(CROSSPREFIX)ar
			CC				= $(CROSSPREFIX)gcc
			CXX				= $(CROSSPREFIX)g++
			LD				= $(CROSSPREFIX)gcc
			NM				= $(CROSSPREFIX)nm
			RANLIB			= $(CROSSPREFIX)ranlib
			STRIP			= $(CROSSPREFIX)strip
		endif
	else ifeq ($(arch),arm)
		# Generic ARM-based Linux toolchain.
		CROSSBASE		= $(LINUX_TOOLS_DIR)
		CROSSBASEREAL	= $(realpath $(CROSSBASE))
		CROSSPREFIX		= $(LINUX_TOOLS_DIR)/bin/arm-none-linux-gnueabihf-
		CROSSROOT		= $(LINUX_TOOLS_SYSROOT_DIR)
		CROSS_INC		= $(CROSSROOT)/usr/include
		CROSS_LIB		= $(CROSSROOT)/lib
		CROSS_ULIB		= $(CROSSROOT)/usr/lib

		CFLAGS			+= -I$(CROSS_INC)
		CPPFLAGS		+= -I$(CROSS_INC)
		LINKFLAGS		+= -L$(CROSS_LIB) -L$(CROSS_ULIB)

		AR				= $(CROSSPREFIX)ar
		CC				= $(CROSSPREFIX)gcc
		CXX				= $(CROSSPREFIX)g++
		LD				= $(CROSSPREFIX)gcc
		NM				= $(CROSSPREFIX)nm
		RANLIB			= $(CROSSPREFIX)ranlib
		STRIP			= $(CROSSPREFIX)strip
	else ifeq ($(arch),arm64)
		# Generic ARM64-based Linux toolchain.
		CROSSBASE		= $(LINUX_TOOLS_DIR)
		CROSSBASEREAL	= $(realpath $(CROSSBASE))
		CROSSPREFIX		= $(LINUX_TOOLS_DIR)/bin/aarch64-none-linux-gnu-
		CROSSROOT		= $(LINUX_TOOLS_DIR)/aarch64-none-linux-gnu/libc
		CROSS_INC		= $(CROSSROOT)/usr/include
		CROSS_LIB		= $(CROSSROOT)/lib64
		CROSS_ULIB		= $(CROSSROOT)/usr/lib64

		CFLAGS			+= -I$(CROSS_INC)
		CPPFLAGS		+= -I$(CROSS_INC)
		LINKFLAGS		+= -L$(CROSS_LIB) -L$(CROSS_ULIB)
	
		AR				= $(CROSSPREFIX)ar
		CC				= $(CROSSPREFIX)gcc
		CXX				= $(CROSSPREFIX)g++
		LD				= $(CROSSPREFIX)gcc
		NM				= $(CROSSPREFIX)nm
		RANLIB			= $(CROSSPREFIX)ranlib
		STRIP			= $(CROSSPREFIX)strip
	else
		# Default toolchain.
		# CROSSLIB		?= /usr/lib
		# CROSS_RROOT		= ""
		# CROSS_RLIB		= usr/lib
	
		# AR				?= ar
		# CC				?= gcc
		# CXX				?= g++
		# LD				?= gcc
		# NM				?= nm
		# RANLIB			?= ranlib
		# STRIP			?= strip
	endif
endif
ifeq ($(debug),1)
	# Override "STRIP" to do nothing in debug builds.
	STRIP				= :
endif

# Paths

SRCROOT					= $(CURDIR)/..
BUILDROOT				= $(SRCROOT)/build/$(config)-$(os)
OBJDIR					= $(BUILDROOT)/obj

COREUTILSROOT			?= $(SRCROOT)/AccessorySDK

VPATH					+= $(SRCROOT)/Platform
VPATH					+= $(SRCROOT)/PlatformPOSIX
VPATH					+= $(SRCROOT)/Sources
VPATH					+= $(SRCROOT)/Support
VPATH					+= $(SRCROOT)/Transport
VPATH					+= $(SRCROOT)/../Examples

# Includes

INCLUDES				+= -I$(SRCROOT)/Sources
INCLUDES				+= -I$(SRCROOT)/Support
INCLUDES				+= -I$(BUILDROOT)
INCLUDES				+= -I$(BUILDROOT)/CoreUtils
INCLUDES				+= -I$(SRCROOT)/../ScreenStream
INCLUDES				+= -I$(SRCROOT)/../gdbus
INCLUDES				+= -I$(SDKTARGETSYSROOT)/usr/include/gstreamer-1.0
INCLUDES				+= -I${SDKTARGETSYSROOT}/usr/include/glib-2.0
INCLUDES				+= -I${SDKTARGETSYSROOT}/usr/lib/glib-2.0/include
INCLUDES				+= -I${SDKTARGETSYSROOT}/usr/include/gio-unix-2.0
INCLUDES				+= -I$(SRCROOT)/../fdk-aac-2.0.2/libAACdec/include
INCLUDES				+= -I$(SRCROOT)/../fdk-aac-2.0.2/libAACenc/include
INCLUDES				+= -I$(SRCROOT)/../fdk-aac-2.0.2/libSYS/include

ifneq ($(nobonjour),1)
MDNSROOT				?= $(SRCROOT)/../mDNSResponder-1310.100.10
INCLUDES				+= -I$(MDNSROOT)/mDNSShared
endif


# Link flags

LINKFLAGS				+= -L$(BUILDROOT)
LINKFLAGS				+= -L$(SRCROOT)/../ScreenStream
LINKFLAGS				+= -L$(SRCROOT)/../Examples/build
LINKFLAGS				+= -L$(SRCROOT)/../fdk-aac-2.0.2/build

ifneq ($(nobonjour),1)
LINKFLAGS				+= -L$(MDNSROOT)/mDNSPosix/build/prod
endif

# Warning flags

COMMON_WARNINGS			+= -W
COMMON_WARNINGS			+= -Wall
COMMON_WARNINGS			+= -Wempty-body
# COMMON_WARNINGS			+= -Werror
COMMON_WARNINGS			+= -Wextra
COMMON_WARNINGS			+= -Wformat
COMMON_WARNINGS			+= -Wmissing-braces
COMMON_WARNINGS			+= -Wmissing-field-initializers
COMMON_WARNINGS			+= -Wno-cast-align
COMMON_WARNINGS			+= -Wno-unused-result
COMMON_WARNINGS			+= -Wparentheses
COMMON_WARNINGS			+= -Wpointer-sign
COMMON_WARNINGS			+= -Wshadow
COMMON_WARNINGS			+= -Wsign-compare
COMMON_WARNINGS			+= -Wswitch
COMMON_WARNINGS			+= -Wundef
COMMON_WARNINGS			+= -Wuninitialized
COMMON_WARNINGS			+= -Wunknown-pragmas
COMMON_WARNINGS			+= -Wunused-function
COMMON_WARNINGS			+= -Wunused-label
COMMON_WARNINGS			+= -Wunused-parameter
COMMON_WARNINGS			+= -Wunused-value
COMMON_WARNINGS			+= -Wunused-variable
COMMON_WARNINGS			+= -Wno-implicit-fallthrough
CWARNS					+= $(COMMON_WARNINGS)
CWARNS					+= -Wmissing-prototypes

CXXWARNS				+= $(COMMON_WARNINGS)
CXXWARNS				+= -Wnon-virtual-dtor
CXXWARNS				+= -Woverloaded-virtual

# Preprocessor flags

COMMONFLAGS				+= -D__STDC_LIMIT_MACROS=1
COMMONFLAGS				+= -D_GNU_SOURCE
COMMONFLAGS				+= -DCFL_BINARY_PLISTS=1
COMMONFLAGS				+= -DCFLITE_ENABLED=1
COMMONFLAGS				+= -DDEBUG_CF_OBJECTS_ENABLED=1
COMMONFLAGS				+= -DDEBUG_EXPORT_ERROR_STRINGS=1
ifeq ($(debug),1)
	COMMONFLAGS			+= -DDEBUG=1
	COMMONFLAGS			+= -O0
	COMMONFLAGS			+= -g
else
	COMMONFLAGS			+= -DNDEBUG
	COMMONFLAGS			+= -Os
endif
COMMONFLAGS				+= -DAES_UTILS_HAS_GLADMAN_GCM=0
ifeq ($(openssl),1)
	COMMONFLAGS			+= -DOPENSSL
	COMMONFLAGS			+= -DOPENSSL_NO_DEPRECATED
else
	COMMONFLAGS			+= -DAES_UTILS_USE_GLADMAN_AES=1
	COMMONFLAGS			+= -DTARGET_HAS_MD5_UTILS=1
	COMMONFLAGS			+= -DTARGET_HAS_SHA_UTILS=1
	COMMONFLAGS			+= -DTARGET_NO_OPENSSL=1
	COMMONFLAGS			+= -DUSE_VIA_ACE_IF_PRESENT=0
endif
COMMONFLAGS				+= -DAIRPLAY_SCREEN=1
COMMONFLAGS				+= -DAUDIO_STREAM_DLL
COMMONFLAGS				+= -DSCREEN_STREAM_DLL

ifeq ($(nobonjour),1)
COMMONFLAGS				+= -DTARGET_NO_BONJOUR=1
endif

# AccessorySDK

COMMONFLAGS				+= -DTARGET_HAS_LIBTOMMATH=1

# Keychain storage

COMMONFLAGS    			+= -DKEYCHAINS_PATH=\"/data/carplay/\"

# Compiler flags

CFLAGS					+= $(INCLUDES) $(COMMONFLAGS) $(CWARNS)
ifneq ($(qnx),1)
	CFLAGS				+= -fgnu89-inline
	CFLAGS				+= -std=gnu99
endif
CXXFLAGS				+= $(INCLUDES) $(COMMONFLAGS) $(CXXWARNS)

# Targets

TARGETS					+= $(BUILDROOT)/libAirPlaySupport.so
TARGETS					+= $(BUILDROOT)/libAirPlay.so
ifeq ($(stub),1)
	TARGETS				+= $(BUILDROOT)/libAudioStream.so
	TARGETS				+= $(BUILDROOT)/libScreenStream.so
	TARGETS				+= $(BUILDROOT)/libAudioConverter.so
endif
TARGETS					+= $(BUILDROOT)/airplayutil

# AirPlay Core

AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayReceiverPOSIX.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayReceiverServer.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayReceiverSession.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayReceiverSessionScreen.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayReceiverSessionAudio.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirPlayUtils.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/AirTunesClock.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/APReceiverStatsCollector.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/APSRTPJitterBuffer.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/SimpleQueue.so.o
AirPlayCore_OBJS		+= $(OBJDIR)/APSRTPPacketHandler.so.o

ifneq ($(nobonjour),1)
AirPlayCore_OBJS		+= $(OBJDIR)/CarPlayControlClient.so.o
endif

# AirPlay

AirPlay_OBJS		+= $(AirPlayCore_OBJS)

# AirPlay Support
AirPlaySupport_OBJS		+= $(OBJDIR)/ScreenUtilsDLL.so.o
AirPlaySupport_OBJS		+= $(OBJDIR)/AudioUtilsDLL.so.o
AirPlaySupport_OBJS		+= $(OBJDIR)/AudioConverterDLL.so.o

# AirPlayUtil

AirPlayUtil_OBJS		+= $(OBJDIR)/airplayutil.o

# Audio / Screen

Audio_OBJS				+= $(OBJDIR)/AudioUtilsALSA.so.o
Screen_OBJS				+= $(OBJDIR)/ScreenUtilsStub.so.o
AudioConverter_OBJS		+= $(OBJDIR)/AudioConverter_FDKAAC_OPUS.so.o

# Libraries

ifeq ($(qnx),1)
	LINKFLAGS			+= -lbacktrace
	LINKFLAGS			+= -lsocket
	LINKFLAGS			+= -lnbutil
endif
ifeq ($(linux),1)
	LINKFLAGS			+= -ldl
	LINKFLAGS			+= -lpthread
	LINKFLAGS			+= -lrt
endif
LINKFLAGS				+= -lc
ifeq ($(openssl),1)
	LINKFLAGS			+= -lcrypto
endif
LINKFLAGS				+= -lm
LINKFLAGS				+= -lasound
LINKFLAGS				+= -lAvcStream
LINKFLAGS				+= -lglib-2.0
LINKFLAGS				+= -lgio-2.0
LINKFLAGS				+= -lgobject-2.0
LINKFLAGS				+= -lfdk-aac

ifneq ($(nobonjour),1)
LINKFLAGS				+= -ldns_sd
endif

# Rules

.PHONY : all
all: $(TARGETS)

$(BUILDROOT)/airplayutil: $(AirPlayUtil_OBJS) $(BUILDROOT)/libAirPlaySupport.so $(BUILDROOT)/libAirPlay.so $(BUILDROOT)/libCoreUtils.so
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) $(LINKFLAGS) -lAirPlaySupport -lAirPlay -lCoreUtils -o $@ $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

# Library rules.

$(BUILDROOT)/libAirPlay.so: $(AirPlay_OBJS) $(BUILDROOT)/libAirPlaySupport.so
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) -shared -Wl,-soname,libAirPlay.so -o $@ $(LINKFLAGS) -lAirPlaySupport $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

$(BUILDROOT)/libAudioStream.so: $(Audio_OBJS) $(BUILDROOT)/libAirPlaySupport.so
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) -shared -Wl,-soname,libAudioStream.so -o $@ $(LINKFLAGS) -lAirPlaySupport $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

$(BUILDROOT)/libAirPlaySupport.so: $(AirPlaySupport_OBJS)
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) -shared -Wl,-soname,libAirPlaySupport.so -o $@ $(LINKFLAGS) $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

$(BUILDROOT)/libScreenStream.so: $(Screen_OBJS) $(BUILDROOT)/libAirPlaySupport.so
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) -shared -Wl,-soname,libScreenStream.so -o $@ $(LINKFLAGS) -lAirPlaySupport $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

$(BUILDROOT)/libAudioConverter.so: $(AudioConverter_OBJS) $(BUILDROOT)/libAirPlaySupport.so
	@echo "Linking ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(CC) -shared -Wl,-soname,libAudioConverter.so -o $@ $(LINKFLAGS) -lAirPlaySupport $^
	$(quiet)$(STRIP) $@
	@echo "$(ColorCyan)=== BUILD COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"
	
.PHONY : clean
clean:
	-rm -rf $(BUILDROOT)

$(AirPlay_OBJS):		| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils
$(AirPlaySupport_OBJS):	| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils
$(AirPlayUtil_OBJS):	| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils
$(Audio_OBJS):			| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils
$(Screen_OBJS):			| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils
$(AudioConverter_OBJS):	| $(BUILDROOT)/obj $(BUILDROOT)/CoreUtils

$(BUILDROOT)/obj:
	$(quiet)mkdir -p $@

# General rules

$(OBJDIR)/%.o: %.c
	@echo "Compiling ($(os)-$(config)-direct) $(ColorMagenta)$(notdir $<)$(ColorEnd)"
	$(quiet)$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

$(OBJDIR)/%.o: %.cpp
	@echo "Compiling ($(os)-$(config)-direct) $(ColorMagenta)$(notdir $<)$(ColorEnd)"
	$(quiet)$(CXX) -c $(CXXFLAGS) -o $@ $<

$(OBJDIR)/%.so.o: %.c
	@echo "Compiling ($(os)-$(config)-shared) $(ColorMagenta)$(notdir $<)$(ColorEnd)"
	$(quiet)$(CC) -c -fPIC $(CPPFLAGS) $(CFLAGS) -o $@ $<

$(OBJDIR)/%.so.o: %.cpp
	@echo "Compiling ($(os)-$(config)-shared) $(ColorMagenta)$(notdir $<)$(ColorEnd)"
	$(quiet)$(CXX) -c -fPIC $(CXXFLAGS) -o $@ $<

# Core Utils

arm					= 1
ifeq ($(openssl),1)
	libtommath		= 0
else
	libtommath		= 1
endif	
mfi					= 1
nvtools				= $(nv)
qnxtools			= $(qnx)

export arch arm debug nohidraw libtommath linux mfi nv nvtools openssl os qnx qnxtools nobonjour rpi
export BUILDROOT
ifeq ($(debug),1)
	export STRIP
endif

.PHONY : $(BUILDROOT)/libCoreUtils.so
$(BUILDROOT)/libCoreUtils.so: $(BUILDROOT)/CoreUtils
	@echo "Making ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(MAKE) -C $(COREUTILSROOT)/PlatformPOSIX -e CoreUtils
	@echo "$(ColorCyan)=== MAKE COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

.PHONY : $(BUILDROOT)/CoreUtils
$(BUILDROOT)/CoreUtils:
	@echo "Copying CoreUtils headers"
	$(quiet)mkdir -p $@
	$(quiet)rsync -ap "$(COREUTILSROOT)/Support/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/Curve25519/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/Ed25519/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/GladmanAES/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/LibTomMath/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/Small25519/"*.h "$(BUILDROOT)/CoreUtils"
	$(quiet)rsync -ap "$(COREUTILSROOT)/External/SRP/"*.h "$(BUILDROOT)/CoreUtils"
	@echo "$(ColorCyan)=== COPY COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"

# Bonjour 

.PHONY : bonjour clean_bonjour
bonjour clean_bonjour:
	@echo "Making ($(os)-$(config)) $(ColorMagenta)$(notdir $@)$(ColorEnd)"
	$(quiet)$(MAKE) -C $(COREUTILSROOT)/PlatformPOSIX -e $@
	@echo "$(ColorCyan)=== MAKE COMPLETE: $(notdir $@) ($(os)-$(config))$(ColorEnd)"
